#ifndef __CONSTANTS_AND_TYPES_H__
#define __CONSTANTS_AND_TYPES_H__

/* 
 * 内容：
 * 	 多模块使用的 自定义类型、宏定义、函数等
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

/* Flag Control */
#define L_aType    0x00000001 	// only used for regist
#define L_Account  0x00000002
#define L_Passwd   0x00000004

#define S_uID      0x00000001   // only used for regist
#define S_Name     0x00000002
#define S_Age      0x00000004
#define S_Tel      0x00000008
#define S_NorBlock S_Name       // Normal user's unchangeable items 
#define S_All      (S_uID|S_Name|S_Age|S_Tel)

/* Buf size */
#define MAX_ACCOUNT_SIZE 21
#define MAX_PASSWD_SIZE  21
#define MAX_NAME_SIZE    21
#define MAX_ACTION_SIZE  21
#define MAX_TIME_SIZE    51

/* string */
#define HEADER        "1:uID, 2:姓名, 3:年龄, 4:电话"
#define TABLE_ACCOUNT "account"
#define ACCOUNT_ITEM  "AccountName TEXT primary key NOT NULL,Passwd TEXT NOT NULL,AccountType INTEGER NOT NULL,EmployeeID INTEGER NOT NULL"
#define TABLE_STAFF   "staff"
#define STAFF_ITEM    "EmployeeID INTEGER primary key NOT NULL,Name TEXT,Age INTEGER,Tel INTEGER"
#define TABLE_HISTORY "history"
#define HISTORY_ITEM  "AccountID INTEGER,Action TEXT,Time TEXT"

/* Universal Printing */
#define PRINT_SPLITE    do {printf("======================================================================\n");} while(0)
#define UNEXCEPT_BRANCH do {printf("%s [%s, %s, %d]\n", "This message should not be dispalyed.", __FILE__, __FUNCTION__, __LINE__);} while(0)
#define SEND_ERR        do {printf("%s [%s, %s, %d]\n", "Failed to send message.", __FILE__, __FUNCTION__, __LINE__);} while(0)
#define RECV_ERR        do {printf("%s [%s, %s, %d]\n", "Failed to receive message.", __FILE__, __FUNCTION__, __LINE__);} while(0)
#define INPUT_ERR       do {printf("输入有误.\n");} while(0)
#define CHOOSE_ERR      do {printf("请输入菜单选项中的数字.\n");} while(0)
#define PRINT_ERR(x)    do {printf("%s:%s [%s, %s, %d]\n", x, strerror(errno), __FILE__, __FUNCTION__, __LINE__);} while(0)


/* Status Control */
typedef enum _Status {
	/* program status */
	Success,
	Failure,
	Stop,      // skip the following sentences 
	Continue,  // execute the following sentences
	/* user commands */
	Quit,
	Login,
	Regist,
	AddStaff,
	DelStaff,
	UpdateInfo,
	SearchInfo,
	History,
	CleanHistory,
} Status;

/* Account Type */
enum AccountType {
	Admin,
	Normal,
};

/* Account Identity */
struct Identity {
	int              a_id; 		// account rowid: improve HistoryTable search efficiency
	enum AccountType a_type;
	int              u_id; 		// employee number
};

/* Login Msg */
typedef struct _LoginMsg {
	struct Identity ID;
	/* account informations */
	char account[MAX_ACCOUNT_SIZE];
	char passwd[MAX_PASSWD_SIZE];
} LoginMsg;

/* Staff Msg */
typedef struct _StaffMsg {
	/* control code */
	struct Identity ID;
	int             flag; 
	/* employee informations */
	int  age;
	int  tel;
	char name[MAX_NAME_SIZE];
} StaffMsg;

/* History Msg */
typedef struct _HistoryMsg {
	/* control code */
	struct Identity ID;
	int             flag;
	/* history informations*/
	char action[MAX_ACTION_SIZE];
	char my_time[MAX_TIME_SIZE];
} HistoryMsg;

#endif
